package org.one.afternoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.ClientUser;
import org.one.afternoon.domain.Movie;
import org.one.afternoon.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/movie")
public class MovieController {
    @Autowired
    private MovieService movieService;

    @CrossOrigin // 跨域访问
    @GetMapping("/all")
    public Result<List<Movie>> findAll() {
        List<Movie> movieList = movieService.list(null);
        return Result.success(movieList);
    }

    @CrossOrigin
    @PostMapping("/queryOne")
    @ResponseBody
    public Result<List<Movie>> queryOne(@RequestBody Movie movie) {
        LambdaQueryWrapper<Movie> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(movie.getTitle()), Movie::getTitle, movie.getTitle())
                .like(StringUtils.isNotBlank(movie.getOriginTitle()), Movie::getOriginTitle, movie.getOriginTitle())
                .like(StringUtils.isNotBlank(movie.getGenre()), Movie::getGenre, movie.getGenre())
                .like(StringUtils.isNotBlank(movie.getTag()), Movie::getTag, movie.getTag())
                .like(StringUtils.isNotBlank(movie.getLanguage()), Movie::getLanguage, movie.getLanguage());
        List<Movie> list = movieService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @CrossOrigin
    @PostMapping("/insertOne")
    @ResponseBody
    public Result<String> addOne(@RequestBody Movie movie) {
        boolean save = movieService.save(movie);
        if (!save) {
            return Result.error("添加用户失败");
        } else {
            return Result.success("添加用户成功");
        }
    }

    @CrossOrigin
    @PostMapping("/deleteOne")
    @ResponseBody
    public Result<String> deleteOne(@RequestParam Integer index) {
        movieService.removeById(index);
        return Result.success("删除成功");
    }

    @CrossOrigin
    @PostMapping("/updateOne")
    @ResponseBody
    public Result<String> updateOne(@RequestBody Movie movie) {
        LambdaUpdateWrapper<Movie> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(StringUtils.isNotBlank(movie.getTitle()), Movie::getTitle, movie.getTitle())
                .set(StringUtils.isNotBlank(movie.getOriginTitle()), Movie::getOriginTitle, movie.getOriginTitle())
                .set(movie.getRatingAverage() != null, Movie::getRatingAverage, movie.getRatingAverage())
                .set(movie.getRatingAverage() != null, Movie::getRatingsCount, movie.getRatingsCount())
                .set(StringUtils.isNotBlank(movie.getDuration()), Movie::getDuration, movie.getDuration())
                .set(StringUtils.isNotBlank(movie.getGenre()), Movie::getGenre, movie.getGenre())
                .set(StringUtils.isNotBlank(movie.getTag()), Movie::getTag, movie.getTag())
                .set(StringUtils.isNotBlank(movie.getTrailerResourceUrl()), Movie::getTrailerResourceUrl, movie.getTrailerResourceUrl())
                .set(movie.getCommentsCount()!=null, Movie::getCommentsCount, movie.getCommentsCount())
                .set(StringUtils.isNotBlank(movie.getImage()), Movie::getImage, movie.getImage())
                .set(StringUtils.isNotBlank(movie.getRatingStars()), Movie::getRatingStars, movie.getRatingStars())
                .set(StringUtils.isNotBlank(movie.getCountry()), Movie::getCountry, movie.getCountry())
                .set(StringUtils.isNotBlank(movie.getDirector()), Movie::getDirector, movie.getDirector())
                .set(StringUtils.isNotBlank(movie.getLanguage()), Movie::getLanguage, movie.getLanguage())
                .set(StringUtils.isNotBlank(movie.getCast()), Movie::getCast, movie.getCast())
                .set(movie.getCommentsId()!=null, Movie::getCommentsId, movie.getCommentsId());

        movieService.update(null, lambdaUpdateWrapper);
        return Result.success("修改成功");
    }
}
